package com.fitz.invoke;

import com.fitz.remote.callback.RpcResponseCallback;
import com.fitz.remote.model.RpcResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fitz
 * Created  on 2020/5/25
 */
public class RpcInvoker {

    private static final Logger logger = LoggerFactory.getLogger(RpcInvoker.class);

    private static volatile RpcInvoker instance = new RpcInvoker();

    private RpcInvoker(){}

    public static RpcInvoker getInstance(){
        return instance;
    }

    private ConcurrentHashMap<String, RpcResponseCallback> rpcResponseCallbackPool = new ConcurrentHashMap<>();

    public void setCallback(String requestId,RpcResponseCallback rpcResponseCallback){
        rpcResponseCallbackPool.put(requestId,rpcResponseCallback);
    }

    public void removeCallback(String requestId){
        rpcResponseCallbackPool.remove(requestId);
    }

    public void notifyCallback(String requestId, RpcResponse rpcResponse){
        RpcResponseCallback callback = rpcResponseCallbackPool.get(requestId);
        if (callback == null){
            return;
        }
        callback.call(rpcResponse);
    }
}
